package com.pearl.jacksoncore.demo.databind.anno;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.ser.VirtualBeanPropertyWriter;
import com.fasterxml.jackson.databind.util.Annotations;

/**
 * @author TD
 * @version 1.0
 * @date 2024/3/28
 */
public class AddressVirtualBeanPropertyWriter extends VirtualBeanPropertyWriter {

    public AddressVirtualBeanPropertyWriter() {
    }

    private AddressVirtualBeanPropertyWriter(BeanPropertyDefinition propDef,
                                           Annotations contextAnnotations, JavaType declaredType) {
        super(propDef, contextAnnotations, declaredType);
    }
    @Override
    protected Object value(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws Exception {
        // 返回虚拟属性对应的值
        return "阿里巴巴";
    }

    @Override
    public VirtualBeanPropertyWriter withConfig(MapperConfig<?> mapperConfig, AnnotatedClass annotatedClass, BeanPropertyDefinition beanPropertyDefinition, JavaType javaType) {
        return new AddressVirtualBeanPropertyWriter(beanPropertyDefinition, null, javaType);
    }
}


